#ifndef Scheduler_h
#define Scheduler_h

#include "Thread.h"
#include "Timer.h"
#include <map>

using std::map;
class Mailer;
class iSchedulable;

typedef struct ThreadStat {
	int ID;
	Thread* thread;
	int starvationTime;
	int time;
	bool status;
} ThreadStat; 

typedef map<int, ThreadStat> THREADTABLE;

class Scheduler {

public:
	Scheduler(void);
	~Scheduler(void);
	enum ThreadState {ZOMBIE=-2,BLOCKED=-1};

	static const int ROUND_ROBIN = 1;
	static const int LONGEST_QUEUE = 2;
	static const int OUR_POLICY = 3;

	void start();
	void stop();
	void Scheduler::addThread(Thread* t, iSchedulable* sched);
	void changePolicy(int policy);
	int crashNode(int id);
	int reviveNode(int id);
	void killCrashed();
	int getTTSize();

	//statistics
	int getNodeStatus(int nodeId);
	void startCounting();
	void stopCounting();
	int getTotalCPU();
	int getNodeCPU(int id);
	void setMailer(Mailer* m);
	void KillAll();
	void startAll();
private:
	THREADTABLE* m_schedulingTable;
	int m_schedulingPolicy;
	int m_SleepTime;
	int m_Quantom;
	bool m_shouldRun;
	int m_totalCPU;
	bool countTime;
	int maxWaitTime;
	Mailer* m_mailer;
	
	void roundRobin();
	void longestQueue();
	void ourPolicy();
	void run(ThreadStat* ts, int quantom);
	map<int, iSchedulable*>* m_scheduables;
};

#endif

